#!/bin/bash
SVNSERVER=$1
PROJECT=$2
FOLDER=$3
VERSION=$4
OPERATOR=$5
export JAVA_HOME=/usr/java/jdk1.7.0_79
export M2_HOME=/usr/local/maven
echo build normal maven bxn project
echo server:$SVNSERVER project:$PROJECT
echo folder:$FOLDER version:$VERSION
CURRENTTIME=$6
TODAY=$(date +%Y%m%d)
if [ ! -d "/opt/build/$TODAY" ];then
	mkdir -p /opt/build/$TODAY
	echo folder $TODAY is not exits,mkdir and  chmod 777 /opt/build/$TODAY
        chmod 777 /opt/build/$TODAY	
fi
echo timestamp:$CURRENTTIME 
rm -rf /tmp/package/$PROJECT-*
BASE=/tmp/package/$PROJECT-$CURRENTTIME
echo base:$BASE
rm -rf $BASE
export LC_CTYPE=en_US.UTF-8
svn export svn://$SVNSERVER/$PROJECT/$FOLDER/$VERSION $BASE --username tiger --password svn555666
cd $BASE
sed -i '8c <version>'$VERSION'</version>' pom.xml
rm -rf /usr/local/maven/repo/cn/boxiao
JAVA_HOME=/usr/java/jdk1.7.0_79
$M2_HOME/bin/mvn package -Dmaven.test.skip
if [ ! -d "target" ]; then
	echo 'target folder not found ,exit.'
	rm -rf $BASE
	exit -1;
fi
WAR=$(ls target | grep [.]war$)
echo package end,war file is `pwd`/target/$WAR
if [ ! -f "target/$WAR" ];then
	echo 'war file is not exist.'
	rm -rf $BASE
	exit -1;
fi
unzip target/$WAR -d target/temp
touch target/temp/dynamic/resources/version.txt
echo $VERSION >> target/temp/dynamic/resources/version.txt
touch manifest
echo Manifest-Version: $VERSION >>manifest
echo Created-By: $OPERATOR >>manifest
jar cvfm  target/temp/WEB-INF/lib/$PROJECT-$VERSION.jar manifest -C target/temp/WEB-INF/classes/ .
rm -rf target/temp/WEB-INF/classes
mkdir dist
cd target/temp
echo begin zip war: `pwd`: `ls`
zip -r ../../dist/$WAR *
cd ../..
mkdir dist/install
cp -r doc dist/install/doc
cp -r script dist/install/script
cd dist
echo begin zip files:  `pwd`:`ls`
zip -r $PROJECT-$VERSION.zip *
cp $PROJECT-$VERSION.zip /opt/build/$TODAY/$PROJECT-$VERSION-$CURRENTTIME-$OPERATOR.zip
cd ../..
rm -rf $BASE
